package com.ctrip.cap.client.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.client.ClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.cap.client.TraversalServiceClient;
import com.ctrip.cap.client.impl.base.BaseClientImpl;
import com.ctrip.cap.client.impl.base.BaseClientImpl.HttpMethodType;
import com.ctrip.cap.client.property.CapClientProperties;
import com.ctrip.cap.domain.traversal.TraversalDomain;
import com.ctrip.cap.domain.traversal.TraversalTarget;

public class TraversalServiceClientImpl extends BaseClientImpl implements TraversalServiceClient{

	private static final Logger logger = LoggerFactory
			.getLogger(TraversalServiceClientImpl.class);
	
	private static TraversalServiceClientImpl client = new TraversalServiceClientImpl();
	
	private TraversalServiceClientImpl(){
		super();
	}
	
	public static TraversalServiceClientImpl getInstance(){
		return client;
	}

	@Override
	public void addJsonFilePathToDB(TraversalDomain data) {
		
		this.baseCall("traversal/service/treepath", data, HttpMethodType.POST);
		
	}

	@Override
	public byte[] downloadTraversalFile(TraversalDomain domain) throws IOException {

		String udid = this.baseCall("traversal/service/treepath", domain, HttpMethodType.GET, String.class, "traversalDomain");
				
		return downloadFile(udid);
	}
	
	@Override
	public List<TraversalDomain> getTraversalInfo(TraversalDomain domain){
		return this.baseCall("traversal/service/info", domain, HttpMethodType.GET, ArrayList.class, "traversalDomain");
	}
	
	@Override
	public List<TraversalTarget> getTraversalTargetInfo(TraversalTarget domain){
		return this.baseCall("traversal/service/target/info", domain, HttpMethodType.GET, ArrayList.class, "traversalDomain");
	}
	
	@Override
	public void migrateTraversalInfoToTargetTemplate(TraversalDomain domain){
		this.baseCall("traversal/service/target/migration", domain, HttpMethodType.PUT);
	}
	
	
	@Override
	public byte[] downloadTaregetFile(TraversalTarget domain) throws IOException {
		
		String udid = this.baseCall("traversal/service/target/path", domain, HttpMethodType.GET, String.class, "traversalDomain");
		
		return downloadFile(udid);
	}
	
	public byte[] downloadFile(String udid) throws IOException {
		
		
		logger.info("request file at " + CapClientProperties.JSON_FILE_DOWNLOAD + "/" + udid + ".txt");
		
		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target(CapClientProperties.JSON_FILE_DOWNLOAD)
				.property(ClientProperties.CONNECT_TIMEOUT,
						CapClientProperties.TIMEOUT_MS).path(udid+".txt");

		Response response = webTarget.request().get();

		InputStream input = (InputStream)response.getEntity();

		byte[] fileBytes = IOUtils.toByteArray(input);  

		return fileBytes;
	}
	
	public static void main(String args[]){
//		TraversalDomain data = new TraversalDomain();
//		data.setCaseID(0);
//		data.setJobID(170185);
//		try {
//			byte[] byts = FileServiceClientImpl.getInstance().downloadFile(data);
//			System.out.println(byts);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
//		TraversalDomain data = new TraversalDomain();
//		data.setJobID(9537);
//	
//			TraversalServiceClientImpl.getInstance().migrateTraversalInfoToTargetTemplate(data);
//			System.out.println(list);

		Client client = ClientBuilder.newClient();
//		WebTarget webTarget = client
//				.target("http://cimobile.dev.sh.ctripcorp.com:8080/job/APP.AT.Benchmark/buildWithParameters?app=http%3A%2F%2Fsvn.dev.sh.ctripcorp.com%2Fsvn%2Fapk%2F%25E4%25B8%25BB%25E7%2589%2588%2F6.0%2FAndroid%2F4.%25e6%25b5%258b%25e8%25af%2595%25e5%258c%2585%2FCtrip_Wireless_View_V6.0_SIT8.2_PRODUCT.apk&Labels=Mobile-Slave-HTC-M8St-1");

		String baseURL = "http://cimobile.dev.sh.ctripcorp.com:8080/job";
		
		String jobName = "/APP.AT.SmokeTest";
		
		int runId = 0;
		
		String packageurl = "http://svn.dev.sh.ctripcorp.com/svn/apk/%E4%B8%BB%E7%89%88/6.2.1/Android/4.%e6%b5%8b%e8%af%95%e5%8c%85/CtripMain_V6.2.1_SIT1_BAOLEI.apk";
		
		List<String> devicesResult = new ArrayList<String>();
		
		devicesResult.add("Mobile-Slave-Samsung-SM-G9008V-1");
		
		int j = 0;
		
		String url = baseURL + jobName

				+ "/buildWithParameters?Params='env=UAT;runid="

				+ runId + ";app=%22" + packageurl + "%22'&Labels='"

				+ devicesResult.get(j) + "'&app=" + packageurl + "&env=UAT&runid=" + runId;

				System.out.println(url);


		
		WebTarget webTarget = client
				.target(url);
		

		Response response = webTarget.request().post(null);
		
//		Client client = ClientBuilder.newClient();
//		WebTarget webTarget = client
//				.target("http://ciapi.dev.sh.ctripcorp.com:8080/RestfulAPI/mobile/InvokeSmokeTest");
//
//		TestDomain domain = new TestDomain();
//		
//		domain.setApp_type("ANDROID");
//		domain.setSitVersion("9");
//		domain.setPackageVersion("test");
//		domain.setProject_name("ANDROID_WIRELESS");
//		domain.setHybrid("UAT");
//		domain.setMobileApptTpe("normal");
//		domain.setHybridversion("602.000");
//		domain.setOtherInfo("");
//		domain.setPackageurl("http://192.168.80.56/ANDROID/2015-01-30/UAT/602.000/9/1714872_135158/CtripMain_V602.000_SIT9_TEST.apk");
//		domain.setAdditional("smokeTest");
//
//		Response response = webTarget.request().post(Entity.entity(domain, MediaType.APPLICATION_JSON));
//		
//		response.getStatus();
//
//		response.getEntity();
		
	}
	
	public static class TestDomain{
		
		private String app_type;
		private String sitVersion;
		private String packageVersion;
		private String project_name;
		private String hybrid;
		private String mobileApptTpe;
		private String hybridversion;
		private String otherInfo;
		private String packageurl;
		private String additional;
		public String getApp_type() {
			return app_type;
		}
		public void setApp_type(String app_type) {
			this.app_type = app_type;
		}
		public String getSitVersion() {
			return sitVersion;
		}
		public void setSitVersion(String sitVersion) {
			this.sitVersion = sitVersion;
		}
		public String getPackageVersion() {
			return packageVersion;
		}
		public void setPackageVersion(String packageVersion) {
			this.packageVersion = packageVersion;
		}
		public String getProject_name() {
			return project_name;
		}
		public void setProject_name(String project_name) {
			this.project_name = project_name;
		}
		public String getHybrid() {
			return hybrid;
		}
		public void setHybrid(String hybrid) {
			this.hybrid = hybrid;
		}
		public String getMobileApptTpe() {
			return mobileApptTpe;
		}
		public void setMobileApptTpe(String mobileApptTpe) {
			this.mobileApptTpe = mobileApptTpe;
		}
		public String getHybridversion() {
			return hybridversion;
		}
		public void setHybridversion(String hybridversion) {
			this.hybridversion = hybridversion;
		}
		public String getOtherInfo() {
			return otherInfo;
		}
		public void setOtherInfo(String otherInfo) {
			this.otherInfo = otherInfo;
		}
		public String getPackageurl() {
			return packageurl;
		}
		public void setPackageurl(String packageurl) {
			this.packageurl = packageurl;
		}
		public String getAdditional() {
			return additional;
		}
		public void setAdditional(String additional) {
			this.additional = additional;
		}
		
		
		
	}

}
